<?php
namespace App\Admin\Controllers;

use App\Admin\Actions\Department\AuditAction;
use App\Models\AdminUser;
use App\Models\Department;
use App\Policies\Admin\DepartmentPolicy;
use Dcat\Admin\Admin;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Grid;

class DjDepartmentsController extends AdminController
{
    public function index(Content $content)
    {
        Admin::style(controllerIndexStyle(100));
        Admin::css(asset('css/admin/uploader_page.css'));
        Admin::css(asset('css/admin/department.css'));

        return $content->body("<div>".$this->grid()."</div>");
    }

    public function grid()
    {
        $filters = request()->all();
        $admin_user = AdminUser::find(Admin::user()->id);
        $is_super_manger = $admin_user->isSuperManger();
        
        $builder = Department::query()->with(['file'])->where('type', Department::TYPE_DJ);
        return Grid::make($builder, function (Grid $grid) use ($is_super_manger, $admin_user, $filters) {
            $grid->model()->orderBy('order', 'asc');
            $grid->column('xh', '序号')->display(function () use ($filters) {
                $per_page = isset($filters['per_page']) ? intval($filters['per_page']) : 20;
                $page = isset($filters['page']) ? intval($filters['page']) : 1;
                $start = ($page - 1) * $per_page;
                return $start + $this->_index + 1;
            })->setHeaderAttributes(['style' => 'width: 60px; text-align: center;'])->style('text-align: center');
            $grid->column('dwmc', '单位')->setHeaderAttributes(['style' => 'width: 20%; text-align: left;'])->style('text-align: left');
            $grid->column('dwdm', '单位')->display(function($name) use ($filters) {
                $per_page = isset($filters['per_page']) ? intval($filters['per_page']) : 20;
                $page = isset($filters['page']) ? intval($filters['page']) : 1;
                $start = ($page - 1) * $per_page;
                $xh= $start + $this->_index + 1;
                $personals = [];
                $fushus = [];
                $department = $this;
                return view('admin.pages.departments.index', compact('xh', 'personals', 'department', 'fushus'));
            });
            $grid->column('status', '状态')->using(Department::$statusMaps)->dot(Department::$statusColorMaps);
            $grid->actions(function (Grid\Displayers\Actions $actions) use ($admin_user) {
                $row = $actions->row;
                $policy = DepartmentPolicy::make($row, $admin_user);
                $actions->disableDelete();
                $actions->disableView();
                $actions->disableEdit();
                $policy->can_verify() && $actions->append(new AuditAction());
                $policy->can_download_zip() && $actions->append('<a href="'.admin_route('department.zip', ['dwdm' => $row->dwdm]).'" target="_blank"><i class="feather icon-download"></i>下载</a>');
            });
            $grid->disableCreateButton();
            $grid->tools(view('admin.grid._grid_title', ['title' => '党建述职材料']));
            $grid->filter(function (Grid\Filter $filter) use ($is_super_manger) {
                $filter->in('dwdm', '单位')->multipleSelect(Department::query()->where('type', Department::TYPE_DJ)->pluck('dwmc', 'dwdm'))->width(3);
            });
            $grid->selector(function (Grid\Tools\Selector $selector) {
                $selector->selectOne('status', '状态', Department::getStatusForSelector(Department::TYPE_DJ));
            });

            $grid->tools('<span class="pull-right"><a target="_blank" href="'.admin_route('department.zip', ['type' => Department::TYPE_DJ, 'download_type' => 'all_ppt']).'" class="btn btn-primary" style="margin-left: 5px;">导出全部PPT文件</a></span>');
            $grid->tools('<span class="pull-right"><a target="_blank" href="'.admin_route('department.zip', ['type' => Department::TYPE_DJ, 'download_type' => 'all_files']).'" class="btn btn-primary" style="margin-left: 5px;">导出全部文件</a></span>');

            $grid->paginate(60);

        });
    }


    
}